/**
 * Copyright 2012 Fonfon.Fenceer
 * All rights reserved.
 * 
 * @project 
 * @author Fonfon.Fenceer
 * @version 1.0
 * @data 2012-3-25
 */
package com.guidecaptain.java.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guidecaptain.java.mapper.UploadifyMapper;
import com.guidecaptain.java.model.pojo.Uploadify;
import com.guidecaptain.java.model.vo.UploadifyVO;
import com.guidecaptain.java.service.UploadService;


/**
 * 
 * @author fenceer
 */
@Service("uploadService")
public class UploadServiceImpl implements UploadService {
	
	private Logger LOG = Logger.getLogger(UploadServiceImpl.class);
	
	@Autowired
	private UploadifyMapper uploadifyMapper;
	
	/**
	 * @param
	 * @return
	 */
	@Override
	public Uploadify insertUploadify(UploadifyVO uploadifyVO) {
		
		return insertUploadify(FileDirectory,uploadifyVO);
	}
	
	/** 
	 * @param 
	 * @return 
	 */
	@Override
	public Uploadify insertUploadify(String fileDirectory, UploadifyVO uploadifyVO) {
		Uploadify uploadify=new Uploadify();
		String saveName=uploadFile(fileDirectory, uploadifyVO.getUploadify(), uploadifyVO.getUploadifyFileName());
	
		if (saveName != null) {
			String fileType = saveName.substring(saveName.lastIndexOf(".")+1);
			uploadify.setFileType(fileType);
			uploadify.setFilePath(fileDirectory + "/" + saveName);
			uploadify.setFileName(uploadifyVO.getUploadifyFileName());
			uploadify.setFileSize(uploadifyVO.getUploadify().length());
			uploadify.setUserId(uploadifyVO.getUserId());
			uploadifyMapper.insertUploadify(uploadify);
		} else {
			System.out.println("<-------File Upload Error!------->");

			return null;// 文件上传失败
		}
		
		return uploadify;
	}

	/**
	 * @param
	 * @return
	 */
	@Override
	public int deleteUploadify(int fileId) {

		return 0;
	}
	
	// 文件上传工具，返回文件存储名称
	private String uploadFile(String fileDirectory, File filedata,String filedataFileName) {
		String saveName;
		if (filedata != null) {
			// 获得upload路径的实际目录(上传储存路径)
			String targetDirectory = ServletActionContext.getServletContext().getRealPath(fileDirectory);
			// 生成保存文件的文件名称
			saveName = generateFileName(filedataFileName);
			// 建立一个目标文件
			File target = new File(targetDirectory, saveName);
			// 将临时文件复制到目标文件
			try {
				FileUtils.copyFile(filedata, target);
			} catch (IOException e) {
				LOG.error(e);

				saveName = null;
			}
		} else {

			saveName = null;
		}
		return saveName;
	}

	// 为上文件自动分配文件名称，避免重复
	private String generateFileName(String FileName) {
		// 获得当前时间
		DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		// 转换为字符串
		String formatDate = format.format(new Date());
		// 随机生成文件编号
		int random = new Random().nextInt(1000);
		// 获得文件后缀名称
		int position = FileName.lastIndexOf(".");
		String extension = FileName.substring(position);
		// 组成一个新的文件名称
		return formatDate + random + extension;
	}

}
